package com.zb.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.rabbitmq.client.*;
import com.zb.client.SkuFeignClient;
import com.zb.config.MQConfig;
import com.zb.entity.TbOrderItemModel;
import com.zb.entity.TbOrderModel;
import com.zb.entity.TbTaskHisModel;
import com.zb.service.TbOrderItemService;
import com.zb.service.TbOrderService;
import com.zb.service.TbTaskHisService;
import com.zb.service.TbTaskService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Component
public class MQListener {

    @Autowired
    private Connection connection;
    @Autowired
    private TbOrderService tbOrderService;
    @Autowired
    private TbOrderItemService tbOrderItemService;

    @Autowired
    private SkuFeignClient skuFeignClient;

    @RabbitListener(queues = MQConfig.PAY_QUEUE)
    public void revicePay(String json){
        System.out.println("监听队列开启...");
        System.out.println(json);
        Map<String,String> map = JSON.parseObject(json, Map.class);
        System.out.println(map.get("out_trade_no"));
        String orderId=map.get("out_trade_no");
        //调用数据执行修改状态
        UpdateWrapper<TbOrderModel>updateWrapper=new UpdateWrapper<>();
        updateWrapper.lambda().eq(TbOrderModel::getId,orderId);
        updateWrapper.lambda().set(TbOrderModel::getPayStatus,"1");
        System.out.println(tbOrderService.update(updateWrapper));
    }

    @RabbitListener(queues = MQConfig.TWO_QUEUE)
    public void reviceTwo(String json) {
        System.out.println("监听死信数据....");
        System.out.println(json);
        TbOrderModel tbOrderModel = JSON.parseObject(json, TbOrderModel.class);
        tbOrderModel = tbOrderService.getById(tbOrderModel.getId());
        if (tbOrderModel.getOrderStatus().equals("1")){
            tbOrderModel.setOrderStatus("2");
            tbOrderService.updateById(tbOrderModel);
            System.out.println("修改订单状态，为支付失败");

            QueryWrapper<TbOrderItemModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TbOrderItemModel::getOrderId,tbOrderModel.getId());
            List<TbOrderItemModel> list = tbOrderItemService.list(queryWrapper);
            for (TbOrderItemModel orderItemModel : list) {
                Integer goodsNum = skuFeignClient.updateGoodsNum(orderItemModel.getSkuId(), -orderItemModel.getNum());
                System.out.println(orderItemModel.getSkuId() + ":回滚库存数量:" + goodsNum);

            }
        }
    }

    public void revicePayMsg() {
        try {
            Channel channel = connection.createChannel();

            DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    //接收服务端发送的数据
                    String data = new String(body,"UTF-8");
                    System.out.println(data);
                    Map<String,String> map = JSON.parseObject(data, Map.class);
                    System.out.println(map.get("out_trade_no"));
                    String orderId=map.get("out_trade_no");
                    //调用数据执行修改状态
                    UpdateWrapper<TbOrderModel>updateWrapper=new UpdateWrapper<>();
                    updateWrapper.lambda().eq(TbOrderModel::getId,orderId);
                    updateWrapper.lambda().set(TbOrderModel::getPayStatus,"1");
                    System.out.println(tbOrderService.update(updateWrapper));

                }
            };
            //监听服务mq服务器上的某个队列
            channel.basicConsume("orderPay", true, defaultConsumer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Autowired
    private TbTaskService tbTaskService;

    @Autowired
    private TbTaskHisService tbTaskHisService;

    @RabbitListener(queues = "addCourseSuccessQueue")
    public void reviceAddCourseSuccess(String json) {
        TbTaskHisModel tbTaskHisModel = JSON.parseObject(json, TbTaskHisModel.class);
        boolean b = tbTaskService.removeById(tbTaskHisModel.getId());
        if (b) {
            tbTaskHisService.save(tbTaskHisModel);
        }
        System.out.println("执行完毕=================");
    }


}
